package com.baimawangzi.recovery.service.impl;

import com.baimawangzi.recovery.bean.Courier;
import com.baimawangzi.recovery.bean.PageBean;
import com.baimawangzi.recovery.dao.CourierDao;
import com.baimawangzi.recovery.dao.impl.CourierDaoImpl;
import com.baimawangzi.recovery.service.CourierService;

import java.util.List;

public class CourierServiceImpl implements CourierService {
    private CourierDao courierDao=new CourierDaoImpl();
    @Override
    public List<Courier> findByLimit(int m, int n) {
        return courierDao.findByLimit(m,n);
    }

    @Override
    public PageBean<Courier> getPageBean(int currentPage, int pageSize) {
        PageBean<Courier> pageBean = new PageBean<>();
        pageBean.setCurrentPage(currentPage);
        pageBean.setPageSize(pageSize);
        pageBean.setTotalSize(courierDao.getCount());
        if (pageBean.getTotalSize()%pageSize==0){
            pageBean.setTotalPage(pageBean.getTotalSize()/pageSize);
        }
        else {
            pageBean.setTotalPage(pageBean.getTotalSize()/pageSize+1);
        }
        int m =(currentPage-1)*pageSize;
        pageBean.setData(courierDao.findByLimit(m,pageSize));
        return pageBean;
    }

    @Override
    public int add(Courier courier) {
        return courierDao.add(courier);
    }

    @Override
    public int delete(int id) {
        return courierDao.delete(id);
    }

    @Override
    public Courier findById(int id) {
        return courierDao.findById(id);
    }

    @Override
    public int update(Courier courier) {
        Courier oldcourier = findById(courier.getId());

        if(!oldcourier.getCouriername().equals(courier.getCouriername())){
            //不让改快递员名
            return 0;
        }

        return courierDao.update(courier);
    }
}
